package leetcode_core.leetcode_1;

import java.util.ArrayList;
import java.util.List;

public class Combine {
    private List<List<Integer>> ans = new ArrayList<List<Integer>>();
    public List<List<Integer>> combine(int n, int k) {
        if(k<=0 || n<=0){
            return ans;
        }
        List<Integer> track = new ArrayList<>();
        backtrack(track,1,n,k);
        return ans;
    }

    private void backtrack(List<Integer> track,int start,int n,int k){
        if(track.size() == k){
            ans.add(new ArrayList<>(track));//copy简洁写法
            return;
        }
        for(int i = start;i<=n;i++){
            //做选择
            track.add(i);
            //递归回溯
            backtrack(track,i+1,n,k);
            //撤销选择
            track.remove(track.size()-1);
        }
    }




}
